package com.ourteam.education.api;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.education.dao.ClassroomDAOQueryBean;
import com.ourteam.education.domain.ClassroomBean;
import com.ourteam.education.service.ICLassroomManageService;
import com.ourteam.frame.controller.AbstractCRUDServiceController;

@Controller
@CrossOrigin
@RequestMapping("/api/EDUCATION/classroom")
public class ClassroomManageAPI extends
		AbstractCRUDServiceController<com.ourteam.education.dao.ClassroomDAOQueryBean, ClassroomSearchBean, com.ourteam.education.domain.ClassroomBean> {

	@Autowired
	private ICLassroomManageService classroomService;

	@Override
	protected ClassroomDAOQueryBean newQueryBean() {
		return new ClassroomDAOQueryBean();
	}

	@Override
	protected int queryCount(ClassroomDAOQueryBean queryBean) throws Exception {
		return classroomService.queryClassroomCount(queryBean);
	}

	@Override
	protected ClassroomBean[] queryData(ClassroomDAOQueryBean queryBean) throws Exception {
		return classroomService.queryClassrooms(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {

		if (ArrayUtils.isEmpty(ids)) {
			return;
		}

		classroomService.deleteClassrooms(ids);
	}

	@Override
	protected ClassroomBean getDataById(long id) throws Exception {

		return this.classroomService.getClassroomById(id);

	}

	@Override
	protected void saveData(ClassroomBean dataBean) throws Exception {

		if (dataBean.getId() == 0) {
			this.classroomService.addNewClassroom(dataBean);
		} else {
			this.classroomService.modifyClassroom(dataBean);
		}
	}

	@RequestMapping("/list")
	@ResponseBody
	public ClassroomBean[] list(final @RequestParam(required = false) String classroomName,
			final @RequestParam(required = false) String status, final @RequestParam(required = false) Long campusId)
			throws Exception {

		ClassroomDAOQueryBean searchBean = new ClassroomDAOQueryBean();

		ClassroomDAOQueryBean.Criteria criteria = searchBean.createCriteria();

		if (StringUtils.isNoneBlank(status)) {
			criteria.andStatusEqualTo(status);
		}

		if (StringUtils.isNoneBlank(classroomName)) {
			criteria.andClassroomNameLike(classroomName);
		}

		if (campusId != null && campusId.longValue() != 0) {
			criteria.andCampusIdEqualTo(campusId);
		}

		return classroomService.queryClassrooms(searchBean);
	}

}
